package ntd.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import cn.hutool.setting.dialect.Props;
import cn.hutool.setting.dialect.PropsUtil;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import ntd.util.frame.data.factory.FactoryData;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
public class ComConfig {

    // nacos client
    public static ConfigService CONFIG_SERVICE;
    // 环境
    public static String LJ_ENV = "env";
    // 本地配置文件
    public static final Props com = new Props("com.properties");

    @Bean
    public DataSource dataSource() {
        return FactoryData.dataSource();
    }

    public static final String NACOS_ADDR = com.getStr("com.nacos.addr");
    public static final String NACOS_USER = com.getStr("com.nacos.user");
    public static final String NACOS_PASS = com.getStr("com.nacos.pass");

    public static final String MQTT_URL = com.getStr("com.mqtt.url");
    public static final String MQTT_USERNAME = com.getStr("com.mqtt.username");
    public static final String MQTT_PASS = com.getStr("com.mqtt.pass");

    public static final String RABBIT_MQ_URL = com.getStr("com.rabbit_mq.url");
    public static final String RABBIT_MQ_USERNAME = com.getStr("com.rabbit_mq.username");
    public static final String RABBIT_MQ_PASSWORD = com.getStr("com.rabbit_mq.password");

    public static final String REDIS_HOST = com.getStr("com.redis.host");
    public static final String REDIS_PORT = com.getStr("com.redis.port");
    public static final String REDIS_PASS = com.getStr("com.redis.pass");
    public static final Integer REDIS_DB_INDEX = com.getInt("com.redis.dbindex");


    public static final String getConfig(String key) {
        try {
            Props props = Props.create();
            String application = init_nacos_client().getConfig("test", "DEFAULT_GROUP", 2000);
            StaticLog.info("nacos 获取配置数据_{}...", application);
            props.load(new ByteArrayInputStream(application.getBytes()));
            return props.getStr(key);
        } catch (NacosException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        StaticLog.info(getConfig("test"));
    }

    private static ConfigService init_nacos_client() {
        if (ObjectUtil.isNull(CONFIG_SERVICE)) {
            // 设置Nacos服务器地址
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.SERVER_ADDR, NACOS_ADDR);
//            properties.put(PropertyKeyConst.USERNAME, NACOS_USER);
//            properties.put(PropertyKeyConst.PASSWORD, NACOS_PASS);
            // 创建ConfigService实例
            try {
                CONFIG_SERVICE = NacosFactory.createConfigService(properties);
            } catch (NacosException e) {
                StaticLog.error(e, "[nacos][连接][{}]失败...", NACOS_ADDR);
            }
        }
        StaticLog.info("LJ_Nacos 服务状态_{}...", CONFIG_SERVICE.getServerStatus());
        return CONFIG_SERVICE;
    }

}
